var EditUser = {};

EditUser.openEditWin = function( id ){
	var form = Ext.create('Ext.form.Panel', {
        border: false,
        fieldDefaults: {
            labelWidth: 80
        },
        defaultType: 'textfield',
        bodyPadding: '10',
        items: [{
            name: 'id', 
            hidden: true   
        },{
            fieldLabel: '用户名',
            name: 'username', 
            anchor:'80%'   
        },{
            fieldLabel: '真实姓名',
            name: 'userCname', 
            anchor: '80%'   
        },{
        	fieldLabel: '密码',
            name: 'password', 
            anchor: '80%'   
        },{
        	fieldLabel: '性别',
            xtype: 'radiogroup',
            layout: 'hbox',
            columns: 2,
            items: [{
            	name: 'sex', 
 				inputValue: '0',
                boxLabel: '女',
                margin: '0 5 0 0',
                checked:true
            }, {
            	name: 'sex', 
 				inputValue: '1',
                boxLabel: '男'
            }],
            anchor: '80%'   
        },{
        	fieldLabel: '电话号码',
            name: 'phone', 
            anchor: '80%'   
        },{
        	fieldLabel: 'E-mail',
            name: 'email', 
            anchor: '80%'   
        },{
        	fieldLabel: '是否有效',
            xtype: 'radiogroup',
            layout: 'hbox',
            columns: 2,
            items: [{
            	name: 'valid', 
            	inputValue: '1',
                boxLabel: '是',
                margin: '0 5 0 0',
                checked:true
            }, {
            	name: 'valid', 
            	inputValue: '0',
                boxLabel: '否'
            }],
            anchor: '80%'   
        }],
        buttons: [{
            text: '保存',
        	handler: function(){
        		Ext.Ajax.request({
                	url: Common.ctx+'/user/saveOrUpdate',
	                params: form.getForm().getValues(),
	                method: 'POST',
	                success: function (response, options) {
//	                    Ext.MessageBox.alert('成功', '从服务端获取结果: ' + response.responseText);
						var json = Ext.JSON.decode(response.responseText);
						if(json.success==false){
							Ext.MessageBox.alert('操作失败', json.message );
						}else{
							
						}
	                },
	                failure: function (res, op) {
	                    Ext.MessageBox.alert('失败');
	                }
            	});
        	}
        },{
            text: '关闭',
        	handler: function(){
        		win.close();
        	}
        }]
    });
    
    var title = '新增用户';
    if( id ){
    	title = '修改用户';
    }
    var win = Ext.create('Ext.window.Window', {
        title: title,
        width: 500, height:300,
        minWidth: 300, minHeight: 200,
        layout: 'fit', plain: true,
        items: form
    });
    win.show();
    //如果是修改，则载入数据
    if( id ){
		Ext.Ajax.request({
        	url: Common.ctx +'/user/getUserById', method: 'POST',
            params: {id:id}, 
            success: function (response, options) {
				var json = Ext.JSON.decode(response.responseText);
				if(json.success==false){
					Ext.MessageBox.alert('操作失败', json.message );
					return ;
				}
				form.getForm().loadRecord(json);
            } 
    	});
            	
//    	var request = new Request();
//		 request.url = 
//		 request.params = {id:id};
//		 request.callBack = function(response){
//			    var formObj = loadForm(userForm,response);
//			    //userForm.getForm().findField('password1').setValue(formObj.password);
//		    	var userFiled = userForm.getForm().findField('username');
//		    	userFiled.setReadOnly(true);
//		    	userFiled.setFieldStyle('background:none;border:0px;');
//    			userForm.getForm().findField('valid').setValue(formObj.valid == 'true'?'1':'0');
//		 };
//		 request.ajax();
//		 userWin.show();
	}
    return win ;
}